<?php
class Po extends CI_Controller{

    private $limit = 10;
	
	function  __construct() {
    parent::__construct();
    $this->load->model('Po_model');
    $this->load->model('Cabang_model');
    $this->load->model('Barang_model');
    $this->load->model('User_model');
	$this->load->model('Penjualan_model');
	$this->load->model('Laporan_model');
	$this->load->model('Stock_barang_model');	
    $this->load->helper(array('form', 'url'));
    $this->load->library(array('form_validation', 'pagination'));
    }

   function index(){

       if ($this->session->userdata('is_logged_in') == TRUE) {
                $this->load->view('admin_views/po/index');

             }
        else {
            redirect('login');
        }
    }
    function home(){

        $this->load->library('pagination'); 
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		$config['base_url'] = site_url('po/home/');
		$config['total_rows'] = $this->Po_model->countData();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']=$offset;
		$data['jumlah_data']=$this->Po_model->countData();

		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

		$data['cabang'] = $nm_cabang[0]->strnama_cabang;
        $data['id_cabang'] = $nm_cabang[0]->intid_cabang;

		$data['po']	= $this->Po_model->get_list_data($this->limit,$offset, $nm_cabang[0]->strnama_cabang);
        $data['user'] = $this->session->userdata('username');

	    $this->load->view('admin_views/po/po', $data);
    }


	//-------------------- Penghitungan stock barang detik itu juga ---------------------//
	public function stock_barang()
    	{
		$user['session'] = $this->session->userdata('username');
		$cabang = $this->User_model->getCabang($user['session']);
		$this->view_halaman_stock($cabang);
    	}
	//Sama persis kyk stock_barang() cmn cabangnya diambil dari cabang yg dipilih bkn dari session
	public function stock_cabang()
	{
		$cabang = $this->User_model->getCabang($_POST['cabang']);
		$this->view_halaman_stock($cabang);
	}
	public function view_halaman_stock($cabang)
	{
		$nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
		
		$data['cabang'] = $nm_cabang[0]->strnama_cabang;
		$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
		
		$nm_cabang = $this->Cabang_model->selectCab();
		foreach ($nm_cabang as $g)
		{
			$data['strnama_cabang'][]	 	= $g->strnama_cabang;
		}
		
		$data['stock_cabang'] = $this->Stock_barang_model->get_kartu_stock($data['id_cabang']);
		
		$this->load->view('admin_views/po/stock_barang', $data);
	}
	public function kunci_stock()
	{
		$this->load->helper('form');
		$this->load->helper('html');
		$this->load->model('stock_barang_model');
		//Delete stock sebelumnya
		$this->stock_barang_model->delete_stock($_POST['id_cabang']);
		//Insert stock yg baru
		$data = $this->input->post('stock');
		for($i=0;$i<=sizeof($data);$i++){
			if(!empty($data[$i]['intid_barang'])){
				$detail = array(
					'intid_barang'	=> $data[$i]['intid_barang'],
					'intid_cabang'	=> $_POST['id_cabang'],
					'set_fisik'		=> $data[$i]['set_fisik'],
					'pcs_fisik'		=> $data[$i]['pcs_fisik'],
					'set_hutang'		=> $data[$i]['set_hutang'],
					'pcs_hutang'		=> $data[$i]['pcs_hutang'],
				);
				$this->stock_barang_model->stock_insert($detail);
			}
		}
		//Update tanggal jadi yg dipilih
		$this->stock_barang_model->date_update($_POST['id_cabang'], $_POST['year'], $_POST['month'], $_POST['day'], $_POST['time']);
		$this->stock_barang();
	}
	//----------------- End of penghitungan stock barang detik itu juga --------------------//


/*
	function pusat(){
		$this->load->library('pagination'); 
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		$config['base_url'] = site_url('po/pusat/');
		$config['total_rows'] = $this->Po_model->countData();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']=$offset;
		$data['jumlah_data']=$this->Po_model->countData();
		
		if($_POST){
			$uri_segment = 4;
			$offset = $this->uri->segment($uri_segment);
			$data['po']	= $this->Po_model->get_list_data_filter($this->limit,$offset, $_POST);	
		}else{
			$data['po']	= $this->Po_model->get_list_data_pusat($this->limit,$offset);
		}
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
            $data['user'] = $this->session->userdata('username');
       $this->load->view('admin_views/po/pusat', $data);
	}
	*/
		////line ikhlas firlana 05042013/////////
	/*
	function pusat(){
		$this->load->library('pagination'); 
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		$config['base_url'] = site_url('po/pusat/');
		$config['total_rows'] = $this->Po_model->countData();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']=$offset;
		$data['jumlah_data']=$this->Po_model->countData();
		
		if($_POST){
			$uri_segment = 4;
			$offset = $this->uri->segment($uri_segment);
			$data['po']	= $this->Po_model->get_list_data_filter($this->limit,$offset, $_POST);	
		}else{
			$data['po']	= $this->Po_model->get_list_data_pusat($this->limit,$offset);
		}
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
            $data['user'] = $this->session->userdata('username');
       $this->load->view('admin_views/po/pusat', $data);
	}
	*/
	function pusat(){
			$limit = 20;
			$uri_segment = 3;
			$offset = $this->uri->segment($uri_segment,0);
			$this->load->library('pagination');
					
			$var1 = $this->input->get("cari");
			//echo("cari : ".$offset.", ".$var1);
			$var2 = $this->input->get("week");
			//echo("cari : ".$offset.", ".$var1);
			$week = $this->Laporan_model->selectWeek();
			foreach ($week as $g)
			{
				$data['id'][]	 	= $g->id;
				$data['intid_week'][] 	= $g->intid_week;
			}
			$jpenjualan = $this->Laporan_model->selectJPenjualan();
			foreach ($jpenjualan as $g)
			{
				$data['intid_jpenjualan'][]	 	= $g->intid_jpenjualan;
				$data['strnama_jpenjualan'][] 	= $g->strnama_jpenjualan;
			}
			if($var1 != null){
				$uri_segment1 = 5;
				$offset1 = $this->uri->segment($uri_segment1);
				
				if($offset1 != null){
					//echo "masuk  kesini <br />";
				}else{
						$limit = 0;
						/*
						$config['uri_segment'] = 4;
						$config['base_url'] = site_url('po/pusat?cari='.$var1.'/');
						$config['total_rows'] =  $this->Po_model->count_table_rows('po');
						$config['per_page'] = $limit;
						$config['num_links'] = 5;
						$config['first_link'] = '<small>awal</small>';
						$config['last_link'] = '<small>akhir</small>';
						$this->pagination->initialize($config);
						*/
						$data['pagination'] = "";
						$data['offset']=$offset;
						$data['jumlah_data']=$this->Po_model->countData();
						
						$data['po']	= $this->Po_model->get_list_data_filter($limit,$offset,$var1,$var2);
						$this->load->view('admin_views/po/pusat_baru', $data);				
				
					}
				/*
				echo("SELECT a.*, cabang.strnama_cabang 
							FROM po a INNER JOIN cabang ON a.intid_cabang = cabang.intid_cabang 
							where a.no_spkb = '' AND cabang.strnama_cabang LIKE '%" . $var1 . "%'  ORDER BY strnama_cabang ASC  LIMIT 0,$limit");
				*/
			}else{				
				$config['uri_segment'] = 3;
				$config['base_url'] = site_url('po/pusat');
				$config['total_rows'] =  $this->Po_model->count_table_rows('po');
				$config['per_page'] = $limit;
				$config['num_links'] = 5;
				$config['first_link'] = '<small>awal</small>';
				$config['last_link'] = '<small>akhir</small>';
				$this->pagination->initialize($config);
				
				$data['pagination'] = $this->pagination->create_links();
				$data['offset']=$offset;
				$data['jumlah_data']=$this->Po_model->countData();
				
				$data['po'] = $this->Po_model->get_list_data_pusat($limit,$offset);	
			    $this->load->view('admin_views/po/pusat_baru', $data);
	}
			
		//$this->load->view('admin_views/po/pusat_baru', $data);
	}
	
	///end//////////////
	function sjpusat(){
	
		$this->load->view('admin_views/po/sjpusat');
	}
	
	 function sjdata(){
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
		$nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
			
		$data['cabang'] = $nm_cabang[0]->strnama_cabang;
		$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
					
		$week = $this->Laporan_model->selectWeek();
		foreach ($week as $g)
		{
			$data['id'][]	 	= $g->id;
			$data['intid_week'][] 	= $g->intid_week;
		}
		
		if($_POST) {
            $this->load->model('PO_model_1','PO1');           
            $id_cabang = $this->input->post('id_cabang');
			$week = $this->input->post('week');
			$data['default'] = $this->Po_model->get_data_sj($id_cabang, $week);
			$query = $this->PO1->OpenDB('select sttb.*, (select strnama_cabang from cabang where intid_cabang = "'.$id_cabang.'")strnama_cabang from sttb where sttb.intid_cabang = "'.$id_cabang." and sttb.intid_week = '".$week.'"');
            $data['default2'] = $query->result();
			$this->load->view('admin_views/po/sjdata', $data);
        }else {
            
            $this->load->view('admin_views/po/sjdata', $data);
        }
		
	}
	
	function sjAdd(){
		
		if ($_POST)
		{
		 	$data['default'] = $this->Po_model->selectSpkb($this->input->post('id_cabang'));
		 	$this->load->view('admin_views/po/sjAdd', $data);
		}else {
		    $this->load->view('admin_views/po/sjAdd');
 		}       
	}
	
	function buat_sj(){
		
		$nm_cabang = $this->Cabang_model->select($this->input->post('intid_cabang'));
        $data['cabang'] = $nm_cabang[0]->strnama_cabang;
		$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
		$getpo = $this->Po_model->get_MaxSjNew();
		$data['maxId'] = $getpo[0]->id;
		$nilai = $getpo[0]->id;
		$id = $nilai + 1;
		$this->Po_model->get_MaxSjUpdate($id);
		$data['week'] = $this->Penjualan_model->selectWeek();
		
		$detail =  $this->input->post('pilih');
		$x = implode(",", $detail);
		$data['default']    = $this->Po_model->get_Barang_stok($x,$data['id_cabang']);
		$this->load->view('admin_views/po/sjCreate', $data);
	}
	
	function sjbiasa(){
		
		$this->load->view('admin_views/po/sjAddBiasa');
 	}
	
	function sjbuat(){
		
		$nm_cabang = $this->Cabang_model->select($this->input->post('id_cabang'));
        $data['cabang'] = $nm_cabang[0]->strnama_cabang;
		$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
		$getpo = $this->Po_model->get_MaxSjNew();
		$data['maxId'] = $getpo[0]->id;
		$nilai = $getpo[0]->id;
		$id = $nilai + 1;
		$this->Po_model->get_MaxSjUpdate($id);
		$data['week'] = $this->Penjualan_model->selectWeek();
		$this->load->view('admin_views/po/sjBuatBiasa', $data);
 	}
	
	function sjSave()
	{
		$id = $this->Po_model->insertSj($_POST);
		$data = $this->input->post('barang');
        for($i=1;$i<=sizeof($data);$i++){
		if(!empty($data[$i]['intid_id'])){
			$detail = array(
							'intid_sj' 			=> $id,
							'intid_barang'	    => $data[$i]['intid_id'],
							'qty'		        => $data[$i]['intquantity']
			);
		$this->Po_model->add_surat_jalan($detail);
	    $this->Penjualan_model->addStok($data[$i]['intid_id'], $data[$i]['intquantity'], 1, 0);
		$this->Po_model->tambahStok($data[$i]['intid_id'], $data[$i]['intquantity'], $this->input->post('intid_cabang'), 0);
		}
        }
		$data['default'] = $this->Po_model->get_cetak_surat_jalan($id);
		$data['week'] = $this->Penjualan_model->selectWeek();
		$this->load->view('admin_views/po/cetak_surat_jalan', $data);
	}
	
	function sjr(){
		
		if ($_POST)
		{
		 	$data['default'] = $this->Po_model->selectSrb($this->input->post('id_cabang'));
		 	$this->load->view('admin_views/po/sjAddRetur', $data);
		}else {
		    $this->load->view('admin_views/po/sjAddRetur');
 		}       
	}
	
	function buat_sjr(){
		
		$detail =  $this->input->post('pilih');
		echo $x = implode(",", $detail);
		$data['default']    = $this->Po_model->get_BarangRetur($x);
		$nm_cabang = $this->Cabang_model->select($this->input->post('intid_cabang'));
        $data['cabang'] = $nm_cabang[0]->strnama_cabang;
		$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
		$getpo = $this->Po_model->get_MaxSjNew();
		$data['maxId'] = $getpo[0]->id;
		$nilai = $getpo[0]->id;
		$id = $nilai + 1;
		$this->Po_model->get_MaxSjUpdate($id);
		$data['week'] = $this->Penjualan_model->selectWeek();
		$this->load->view('admin_views/po/sjCreateRetur', $data);
	}
	
	function poAdd(){
		if (!$_POST)
		{
			
			$cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$data['week'] = $this->Penjualan_model->selectWeek();
			$data['user'] = $this->session->userdata('username');
			$getpo = $this->Po_model->get_MaxPoNew();
			$data['maxId'] = $getpo[0]->id;
			$nilai = $getpo[0]->id;
			$id = $nilai + 1;
			$this->Po_model->get_MaxPoUpdate($id);
			$this->load->view('admin_views/po/add', $data);
		 
		 }else{

           $ins = $this->Po_model->insert($_POST);
			//$id ="";
            $id = $ins;
			
            $data = $this->input->post('barang');
			$track = $this->input->post('tracker');
			for($i = 1; $i <= $track;$i++){
				if(!empty($data[$i]['intid_id'])){
					$detail = array(
								'intid_po' 		=> $id,
								'intid_barang'	=> $data[$i]['intid_id'],
								'qty'	        => $data[$i]['intquantity'],
								'ket'		    => $data[$i]['ket']
								);
				//echo "intid_po ".$id."<br />intid_barang ".$data[$i]['intid_id']."<br />qty ".$data[$i]['intquantity']."<br />".$data[$i]['ket']."<br />--------------------<br />";
				$this->Po_model->add($detail);
				}
			}
            /*
			for($i=1;$i<=sizeof($data);$i++){
            if(!empty($data[$i]['intid_id'])){
            $detail = array(
                            'intid_po' 		=> $id,
                            'intid_barang'	=> $data[$i]['intid_id'],
                            'qty'	        => $data[$i]['intquantity'],
							'ket'		    => $data[$i]['ket']
                            );

					//$this->Po_model->add($detail);
					echo "intid_po ".$id."<br />intid_barang ".$data[$i]['intid_id']."<br />qty ".$data[$i]['intquantity']."<br />".$data[$i]['ket']."<br />--------------------<br />";
				}
            } 
			*/

			redirect('po/cetak_po');
		}

	}

    function cetak_po(){
        $max = $this->Po_model->get_MaxPo();
        $id = $max->intid_po;
        $ada = $this->Po_model->get_CetakPo($id);
        $data['default'] = $this->Po_model->get_CetakPo($id);
		$data['week'] = $this->Penjualan_model->selectWeek();
        $this->load->view('admin_views/po/cetak_po', $data);
	}
	
	function cetak_po_view($id){
        $ada = $this->Po_model->get_CetakPo($id);
        $data['default'] = $this->Po_model->get_CetakPo($id);
		$data['week'] = $this->Penjualan_model->selectWeek();
        $this->load->view('admin_views/po/cetak_po_pusat', $data);
	}
	
	function cetak_surat_keluar($id){
        $data['default'] = $this->Po_model->get_CetakPo($id);
		
		$data['week'] = $this->Penjualan_model->selectWeek();
		$data['surat_keluar'] = "surat_keluar";
		
		$cek = $this->Po_model->cek_spkb($data['default'][0]->intid_po);
		if(empty($cek->spkb)){
				$spkb =  $data['default'][0]->intid_po."/".$data['week'][0]->intid_week."/SPKB/".date('Y');
				$this->Po_model->update_spkb($spkb,$data['default'][0]->intid_po);
		}
        $this->load->view('admin_views/po/cetak_po', $data);
	}
	
	function cetak_sttb($id){
        $data['default'] = $this->Po_model->get_CetakRetur($id);
		
		$data['week'] = $this->Penjualan_model->selectWeek();
		$data['surat_keluar'] = "surat_keluar";
		
		$cek = $this->Po_model->cek_sttb($data['default'][0]->intid_retur);
		if(empty($cek->sttb)){
				$sttb =  $data['default'][0]->intid_retur."/".$data['week'][0]->intid_week."/STTB/".date('Y');
				$this->Po_model->update_sttb($sttb,$data['default'][0]->intid_retur);
		}
        $this->load->view('admin_views/po/cetak_surat_retur', $data);
	}
	
	function sj() {

		$this->load->library('pagination'); 
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		$config['base_url'] = site_url('po/sj/');
		$config['total_rows'] = $this->Po_model->countData();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']=$offset;
		
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

        $data['cabang'] = $nm_cabang[0]->strnama_cabang;
        $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
		$data['jumlah_data']=$this->Po_model->countData();
		
		$data['po']	= $this->Po_model->get_list_data_sj($this->limit,$offset, $cabang[0]->intid_cabang);
        $data['user'] = $this->session->userdata('username');
        $week = $this->Po_model->selectWeek();
		foreach ($week as $g)
		{
			$data['id'][]	 	= $g->id;
			$data['intw'][] 	= $g->intid_week;
		}
		$this->load->view('admin_views/po/sj', $data);

	}
	
	function sretur()
	{
		$this->load->view('admin_views/po/sretur');
	}
	
	function returAdd(){
		if (!$_POST)
		{

            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$data['week'] = $this->Penjualan_model->selectWeek();
			$week = $this->Po_model->selectWeek();
			foreach ($week as $g)
			{
				$data['id'][]	 	= $g->id;
				$data['intw'][] 	= $g->intid_week;
			}
			
			$getretur = $this->Po_model->get_MaxReturNew();
			$data['maxId'] = $getretur[0]->id;
			$nilai = $getretur[0]->id;
			$id = $nilai + 1;
			$this->Po_model->get_MaxReturUpdate($id);
            $data['user'] = $this->session->userdata('username');
			
			$this->load->view('admin_views/po/retur_add', $data);
		 }else{

            $ins = $this->Po_model->insert_surat_retur($_POST);

            $id = $ins;
            $data = $this->input->post('barang');
            for($i=1;$i<=sizeof($data);$i++){
            if(!empty($data[$i]['intid_id'])){
            $detail = array(
                            'intid_retur' 	=> $id,
                            'intid_barang'	=> $data[$i]['intid_id'],
                            'qty'		    => $data[$i]['intquantity'],
							'ket'			=> $data[$i]['ket']
                            );

             //var_dump($detail[$i]['intid_id']);
			 $this->Po_model->add_surat_retur($detail);
			 $this->Po_model->minusStok($data[$i]['intid_id'], $data[$i]['intquantity'], $this->input->post('intid_cabang'));
			 $this->Po_model->addStokPusat($data[$i]['intid_id'], $data[$i]['intquantity']);		 
            }
            }

			redirect('po/cetak_surat_retur/'.$id);
		}

	}
	
	function surat_retur() {

		$this->load->library('pagination'); 
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		$config['base_url'] = site_url('po/surat_retur/');
		$config['total_rows'] = $this->Po_model->countData();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']=$offset;
		$data['jumlah_data']=$this->Po_model->countDataRetur();
		
                
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$data['po']	= $this->Po_model->get_list_data_retur($this->limit,$offset, $nm_cabang[0]->intid_cabang);

            $data['user'] = $this->session->userdata('username');
            $week = $this->Po_model->selectWeek();
			foreach ($week as $g)
			{
				$data['id'][]	 	= $g->id;
				$data['intw'][] 	= $g->intid_week;
			}
		$this->load->view('admin_views/po/retur', $data);

	}
	
	function kartu_stok()
    {
	
		if (!$_POST)
		{	
		    $cabang = $this->User_model->getCabang($this->session->userdata('username'));
			$nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
	
			$data['cabang'] = $nm_cabang[0]->strnama_cabang;
			$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$this->load->view('admin_views/po/kartu_stok', $data);
		} else {
			
			$cabang = $this->User_model->getCabang($this->session->userdata('username'));
			$nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
	
			$data['cabang'] = $nm_cabang[0]->strnama_cabang;
			$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			
			$uri_segment = 3;
			$offset = $this->uri->segment($uri_segment);
			$config['base_url'] = site_url('po/view_kartustok/');
			$config['total_rows'] = $this->Po_model->countData();
			$config['per_page'] = $this->limit;
			$config['num_links'] = 2;
			$config['full_tag_open'] = '<div id="pagination">';
			$config['full_tag_close'] = '</div>';
	
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();
			$data['offset']=$offset;
			
			$id_cabang = $this->input->post('id_cabang');
			$tgl = $this->input->post('datetgl');
			$id_barang = $this->input->post('id_barang');
			$data['po'] = $this->Po_model->getKartu_Stok($id_cabang, $tgl, $id_barang);
			$data['id_cabang'] = $id_cabang;
			$data['tgl'] = $tgl;
			$data['id_barang'] = $id_barang;			
			$this->load->view('admin_views/po/kartu_stok', $data);
			
		}
	}
		/*
	function lap_stok_cab()
    {
		    
			$cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
			$data['intid_user'] = $cabang[0]->intid_user;
			$data['intid_privilege'] = $cabang[0]->intid_privilege;

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$week = $this->Po_model->selectWeek();
			foreach ($week as $g)
			{
				$data['id'][]	 	= $g->id;
				$data['intw'][] 	= $g->intid_week;
			}  
		
		if (!$_POST)
		{
            $data['user'] = $this->session->userdata('username');
            $data['po'] = "";
            $this->load->view('admin_views/po/lap_stok_cab', $data);
		 
		 }else{
					
			$intid_cabang = $this->input->post('intid_cabang');
			$intid_week = $this->input->post('intid_week');
			$nama = $this->input->post('nama');
			$data['id_cabang'] = $intid_cabang;
			$data['intid_week'] = $intid_week;
			$data['user'] = $this->session->userdata('username');
			$nm_cabang = $this->Cabang_model->select($intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['po'] = $this->Po_model->getLap_Stok_cab($intid_cabang, $intid_week);
			
			$this->load->view('admin_views/po/cetak_lap_stok_cab', $data);
		 }
       
        }
		*/
		//PO
function lap_stok_cab()
    {
		    $this->load->model('PO_model_1','PO');
			$cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
			$data['intid_user'] = $cabang[0]->intid_user;
			$data['intid_privilege'] = $cabang[0]->intid_privilege;

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$week = $this->PO->selectWeek2($data['id_cabang']);
			
			if(!isset($data['id'])){
				$data['id'][]	 	= "START";
				$data['intw'][] 	= "START";
			}
			foreach ($week as $g)
			{
				$data['id'][]	 	= $g->id;
				$data['intw'][] 	= $g->intid_week;
			}  
		
		if (!$_POST)
		{
            $data['user'] = $this->session->userdata('username');
            $data['po'] = "";
            $this->load->view('halaman/ST_hal2', $data);
		 
		 }else{
			/*		
			$intid_cabang = $this->input->post('intid_cabang');
			$intid_week = $this->input->post('intid_week');
			$nama = $this->input->post('nama');
			$data['id_cabang'] = $intid_cabang;
			$data['intid_week'] = $intid_week;
			$data['user'] = $this->session->userdata('username');
			$nm_cabang = $this->Cabang_model->select($intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            //$data['po'] = $this->Po_model->getLap_Stok_cab($intid_cabang, $intid_week);
			*/
			echo "ERROR!";
			//$this->load->view('admin_views/po/cetak_lap_stok_cab', $data);
		 }
       
        }
	
		function Lap_stock_op() {
		
			$cabang = $this->User_model->getCabang($this->session->userdata('username'));
			$nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
	
			$data['cabang'] = $nm_cabang[0]->strnama_cabang;
			$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
	
	
			$data['user'] = $this->session->userdata('username');
		
			if ($_POST)
			{
				$this->load->library('pagination'); 
				$uri_segment = 3;
				$offset = $this->uri->segment($uri_segment);
				$config['base_url'] = site_url('po/lap_stok_cab/');
				$config['total_rows'] = $this->Po_model->countData();
				$config['per_page'] = $this->limit;
				$config['num_links'] = 2;
				$config['full_tag_open'] = '<div id="pagination">';
				$config['full_tag_close'] = '</div>';
		
				$this->pagination->initialize($config);
				$data['pagination'] = $this->pagination->create_links();
				$data['offset']=$offset;
				
				$intid_cabang = $this->input->post('intid_cabang');
				$tgl = $this->input->post('datetgl');
				$nama = $this->input->post('nama');
				$data['id_cabang'] = $intid_cabang;
				$data['user'] = $this->session->userdata('username');
				$data['po'] = $this->Po_model->getLap_Stok_Op($this->limit, $offset, $intid_cabang, $tgl);
			}
		$this->load->view('admin_views/po/stok_offname', $data);
	}
	
	function view_po($id) {
		$data['po']	= $this->Po_model->get_list_data_detail($id);
        $this->load->view('admin_views/po/view_detail_po', $data);
    }
	
	function cetak_sj(){
        
		$data['week'] = $this->Penjualan_model->selectWeek();
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
        $id_cabang = $nm_cabang[0]->intid_cabang;
		$tgl = date("Y-m-d");
		$max = $this->Po_model->get_MaxSj();
		if(empty($max->intid_sj)){
			$maxId =1;
		} else {
			$maxId = $max->intid_sj + 1;
		}
		$po = $this->input->post('spkb');
		$spkb = $this->Po_model->spkb($po);
		$no_spkb = $spkb[0]->no_spkb;
		$no_sj = $maxId."/".$data['week'][0]->intid_week."/SJ/".date('Y');
		$sj = array(
                            'no_sj' 		=> $no_sj,
                            'no_spkb'		=> $no_spkb,
                            'intid_cabang'	=> $id_cabang,
							'intid_week'	=> $data['week'][0]->intid_week,
							'datetgl'		=> $tgl
							
							);
		$ins = $this->Po_model->insert_surat_jalan($sj);
       	$this->Po_model->update_po($po);
		$detail =  $this->input->post('detail');
		$count_detail = count($detail);
		
			for($i=0; $i<$count_detail; $i++)
			{
				if(isset($detail[$i]['intid_barang'])){
					$_data['intid_sj'] 				= $ins;
					$_data['intid_barang'] 			= $detail[$i]['intid_barang'];
					$_data['reg_pcs'] 				= $detail[$i]['reg_pcs'];					
					$_data['reg_set'] 				= $detail[$i]['reg_set'];
					$_data['free_pcs'] 				= $detail[$i]['free_pcs'];
					$_data['free_set'] 				= $detail[$i]['free_set'];
					$_data['ket'] 					= $detail[$i]['ket'];
					
					$this->Po_model->add_surat_jalan($_data);
					$this->Po_model->addStok($detail[$i]['intid_barang'], $detail[$i]['reg_pcs'], $detail[$i]['reg_set'], $detail[$i]['free_pcs'], $detail[$i]['free_set'], $id_cabang);
				}
			}
		$data['default'] = $this->Po_model->get_cetak_surat_jalan($ins);
		$data['week'] = $this->Penjualan_model->selectWeek();
		$this->load->view('admin_views/po/cetak_surat_jalan', $data);
	}
	
	function view_sj($id) {
		$data['default'] = $this->Po_model->get_cetak_surat_jalan($id);
		$data['week'] = $this->Penjualan_model->selectWeek();
		$data['id'] = $id;
		$this->load->view('admin_views/po/cetak_surat_jalan', $data);
    }
	
	function lookupBarang(){
	$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Po_model->selectBarang($keyword);
        if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
                                        'value' => $row->strnama
                                     );
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data);
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	
	function lookupBarangSj(){
	$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Po_model->selectBarangSj($keyword);
        if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
                                        'value' => $row->strnama,
                                        'value1'=> $row->intqty_end
                                     );
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data);
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	
     function lookupBarangRetur(){
		$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Po_model->selectBarangRetur($keyword);
        if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
                                        'value' => $row->strnama,
                                        'value1' => $row->strnama_jsatuan,
                                        ''
                                     );
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data);
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	
	function lookupCabang(){
		$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Po_model->selectCabang($keyword);
        if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {
                $data['message'][] = array(
                                        'id'=>$row->intid_cabang,
                                        'value' => $row->strnama_cabang,
                                        ''
                                     );
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data);
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	
	function cetak_surat_retur($id){
		$data['default'] = $this->Po_model->get_cetak_surat_retur($id);
		$data['week'] = $this->Penjualan_model->selectWeek();
        $this->load->view('admin_views/po/cetak_surat_retur', $data);
	}
	

	function sreturpusat(){
		$this->load->library('pagination'); 
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		$config['base_url'] = site_url('po/sreturpusat/');
		$config['total_rows'] = $this->Po_model->countDataRetur();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']=$offset;
		$data['jumlah_data']=$this->Po_model->countDataRetur();
		
		if($_POST){
			$data['po']	= $this->Po_model->get_list_data_filter_retur($this->limit,$offset, $_POST);	
		}else{
			$data['po']	= $this->Po_model->get_list_retur_data($this->limit,$offset);
		}
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;


            $data['user'] = $this->session->userdata('username');
        $this->load->view('admin_views/po/returpusat', $data);
	}
	
	function view_retur($id) {
		$data['po']	= $this->Po_model->get_list_data_retur_detail($id);
        $this->load->view('admin_views/po/view_detail_retur', $data);
    }
	
	function get_barang_by_srb($srb){
		
		$this->db->flush_cache();
		$this->db->from('retur');
		$this->db->where('intid_retur', $srb);
		$que1 = $this->db->get();
		
		if($que1->num_rows() > 0){
			$intid_retur = $que1->row()->intid_retur;
			
			$this->db->flush_cache();
			$this->db->select('*, barang.strnama');
			$this->db->from('retur');
			$this->db->join('retur_detail', 'retur_detail.intid_retur = retur.intid_retur');
			$this->db->join('barang', 'barang.intid_barang = retur_detail.intid_barang');
			$this->db->where('retur.intid_retur', $intid_retur);
			$que2 = $this->db->get();
			
			if($que2->num_rows() > 0){
				$i=0;
				foreach($que2->result() as $row){
					echo
							'<tr>
								<td>
									<input name="detail['.$i.'][intid_detail_retur]" value="'.$row->intid_detail_retur.'" type="hidden" />
									<input name="detail['.$i.'][intid_barang]" value="'.$row->intid_barang.'" type="checkbox" />
								</td>
								<td>'.$row->strnama.'</td>
								<td><input name="detail['.$i.'][reg_pcs]" value="'.$row->reg_pcs.'" type="hidden" />'.$row->reg_pcs.'</td>
								<td><input name="detail['.$i.'][reg_set]" value="'.$row->reg_set.'" type="hidden" />'.$row->reg_set.'</td>
								<td><input name="detail['.$i.'][free_pcs]" value="'.$row->free_pcs.'" type="hidden" />'.$row->free_pcs.'</td>
								<td><input name="detail['.$i.'][free_set]" value="'.$row->free_set.'" type="hidden" />'.$row->free_set.'</td>
								<td><input name="detail['.$i.'][ket]" value="'.$row->ket.'" type="hidden" />'.$row->ket.'</td>
							</tr>'
					;
					
					$i++;
				}
			}
		}
	}
	
	function view_kartustok() {
		$id_cabang = $this->input->post('id_cabang');
		$tgl = $this->input->post('tgl');
		$id_barang = $this->input->post('id_barang');
		$data['po'] = $this->Po_model->getKartu_Stok($id_cabang, $tgl, $id_barang);
        $this->load->view('admin_views/po/view_kartustok', $data);
    }
	
	function preview_opname(){
		
		$pilih = $this->input->post('pilih');
		$nama = $this->input->post('nama');
		$pcsakhir = $this->input->post('pcsakhir');
		$setakhir = $this->input->post('setakhir');
		
		$fisikpcs = $this->input->post('fisikpcs');
		$fisikset = $this->input->post('fisikset');
		
		$selisihpcs = $this->input->post('selisihpcs');
		$selisihset = $this->input->post('selisihset');
		 		
		
		for($a=1;$a<=count($pilih);$a++){
			if(!empty($pilih[$a])){
				$data['op'][] = array(
				'nama' => $nama[$a],
				'pcsakhir' => $pcsakhir[$a],
				'setakhir' => $setakhir[$a],
				'fisikpcs' => $fisikpcs[$a],
				'fisikset' => $fisikset[$a],
				'selisihpcs' => $selisihpcs[$a],
				'selisihset' => $selisihset[$a]
				
				);
				$this->Po_model->endStok($pilih[$a], $selisihpcs[$a], $selisihset[$a], $this->input->post('intid_cabang'));
			}
		}
		
		$this->load->view('admin_views/po/preview_opname', $data);
	}
	
	function rekapkeluarbarang() {
		
		$this->load->view('admin_views/po/rekapkeluarbrg');
    }
	
	function rkbcabang(){
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
		$data['user'] = $cabang[0]->intid_user;
		$data['intid_privilege'] = $cabang[0]->intid_privilege;

        $data['cabang'] = $nm_cabang[0]->strnama_cabang;
		$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
		$week = $this->Laporan_model->selectWeek();
		foreach ($week as $g)
		{
			$data['id'][]	 		= $g->id;
			$data['intid_week'][] 	= $g->intid_week;
		}
		$this->load->view('admin_views/po/param_keluarbrg', $data);
	}
	
	function cetak_brgmingguan()
	{
		$data['week']= $this->input->post('intid_week');
		$nm_cabang = $this->Cabang_model->select($this->input->post('intid_cabang'));
        $data['cabang'] = $nm_cabang[0]->strnama_cabang;
		$data['default'] = $this->Po_model->get_CetakPengeluaranBarangMingguan($this->input->post('intid_week'), $this->input->post('intid_cabang'));
        $this->load->view('admin_views/po/keluar_barang_mingguan', $data);
	}
	
	function rkbpusat(){
		$week = $this->Laporan_model->selectWeek();
		foreach ($week as $g)
		{
			$data['id'][]	 		= $g->id;
			$data['intid_week'][] 	= $g->intid_week;
		}
		$this->load->view('admin_views/po/param_keluarbrgpusat', $data);
	}
	
	function cetak_brgmingguanpusat()
	{
		$data['week']= $this->input->post('intid_week');
		$data['default'] = $this->Po_model->get_CetakPengeluaranBarangMingguanPusat($this->input->post('intid_week'));
        $this->load->view('admin_views/po/keluar_barang_mingguanpusat', $data);
	}
	
	function inputstokpusat(){
	
		if($_POST) {
            $detail =  $this->input->post('pilih');
			for($i=0; $i<count($detail); $i++)
			{
				$this->Po_model->inputStok($detail[$i]);
			}
					
			
			//redirect('po');
        }else {
            
            $data['barang'] = $this->Po_model->getBarang();
			$this->load->view('admin_views/po/input_stok', $data);
        }
		
	}
        // stok pusat

    function stok(){
	
		$week = $this->Po_model->selectWeek();
		foreach ($week as $g)
		{
			$data['id'][]	 	    = $g->id;
			$data['intid_week'][] 	= $g->intid_week;
		}
		
		if($_POST) {
                        
            $intid_barang= $this->input->post('intid_barang');
            $week = $this->input->post('week');
            $data['default'] = $this->Po_model->get_data_stok($intid_barang, $week);
            $this->load->view('admin_views/po/stok_pusat', $data);
        }else {
            
            $this->load->view('admin_views/po/stok_pusat', $data);
        }
		
	}
	

//kartu stok pusat
    function kspusat()
    {
	if($_POST) {
           $intid_barang= $this->input->post('intid_barang');
           $tgl = $this->input->post('datetgl');
           $data['po'] = $this->Po_model->getKartu_stok($intid_barang, $tgl);
           $this->load->view('admin_views/po/ks_pusat',$data);

           }else {
                 
			$this->load->view('admin_views/po/ks_pusat');
           }
	}

	function rekap(){
		$this->load->library('pagination');
		$uri_segment = 3;
		$offset = $this->uri->segment($uri_segment);
		$config['base_url'] = site_url('po/pusat/');
		$config['total_rows'] = $this->Po_model->countData();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']=$offset;
		$data['jumlah_data']=$this->Po_model->countData();

		if($_POST){
			$data['po']	= $this->Po_model->get_list_data_filter($this->limit,$offset, $_POST);
		}else{
			$data['po']	= $this->Po_model->get_list_data_pusat($this->limit,$offset);
		}
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;


            $data['user'] = $this->session->userdata('username');
       $this->load->view('admin_views/po/rekap', $data);
	}
	
	function save_sjpo()
	{
		/*
		$id = $this->Po_model->insertSj($_POST);
		$id_barang = $this->input->post('pilih');
		$qty = $this->input->post('quantity');
		$ket = $this->input->post('keterangan');
		for($i=0;$i<sizeof($id_barang);$i++) {	
				if(!empty($qty[$i]) or $qty[$i] > 0 or !empty($id_barang[$i])){
					//udah sukses
					$quantity = $qty[$i];
					$keterangan = $ket[$i];
					$arr_chk = explode("_",$id_barang[$i]);
					//update PO
					$where_pr['intid_barang'] = $arr_chk[0];
					$where_pr['intid_po'] = $arr_chk[1];
					$update_pr['status'] = 1;
					$update_pr['qty_kirim'] = $quantity;
					$this->Po_model->updatePo($where_pr, $update_pr);
					//update SJ
					$update_po['is_sj'] = 1;
					$where_po['intid_po'] = $arr_chk[1];
					$this->Po_model->updatePoSj($where_po, $update_po);
					//Insert surat jalan detail
					$insert_sj['intid_barang']= $arr_chk[0];
					$insert_sj['intid_sj']= $id;
					$insert_sj['qty']= $quantity;
					$insert_sj['ket']= $keterangan;
					
					$this->Po_model->add_surat_jalanid($insert_sj);
					//berkurangnya stok pusat 
					//$qty = $arr_chk[3];
					//$where_getstok['intid_barang'] = $arr_chk[0];
					//$where_getstok['intid_cabang'] = 1;
					$intid_barang = $arr_chk[0];
					//$intid_cabang = 1;
					$get_stokpusat = $this->Po_model->get_stokpusat($intid_barang);
					//print_r($get_stokpusat)
					//$qtyendbefore = $get_stokpusat->row()->intqty_end;
					$qtyendbefore = $get_stokpusat[0]->intqty_end;
					//echo $qtyendbefore;
					$update_stokpusat['intqty_end'] = $qtyendbefore - $quantity;
					$update_stokpusat['tanggal']=  date("Y-m-d");
					$update_stokpusat['intqty_out']=  $quantity;
					$update_stokpusat['intid_cabang'] = 1;
					$week = $this->Penjualan_model->selectWeek();
					$update_stokpusat['intid_week'] = $week[0]->intid_week;
					$update_stokpusat['intid_barang'] = $arr_chk[0];
					$update_stokpusat['intqty_begin'] = $qtyendbefore;
					$where_stokpusat['intid_barang'] = $arr_chk[0];
					$where_stokpusat['intid_cabang'] = 1;
					$this->Po_model->updateStokPusat($where_stokpusat, $update_stokpusat);
					$this->Po_model->insertstokhistory($update_stokpusat);
					//bertambahnya stok cabang yang di kirim sj
					//$where_getstokcab['intid_barang'] = $arr_chk[0];
					//$qty = $arr_chk[3];
					//$update_stokcab['intid_cabang'] = $arr_chk[2];
					$intid_barang = $arr_chk[0];
					$intid_cabang = $arr_chk[2];
					$get_stokcab = $this->Po_model->get_stokcab($intid_barang, $intid_cabang);
					//$qtyendbefore = $get_stokcab->row()->intqty_end;
					$qtyendbeforecab = $get_stokcab[0]->intqty_end;
					$update_stokcab['intqty_end'] = $qtyendbeforecab + $quantity;
					$update_stokcab['tanggal']=  date("Y-m-d");
					$update_stokcab['intqty_in']=  $quantity;
					$update_stokcab['intid_cabang'] = $arr_chk[2];
					$week = $this->Penjualan_model->selectWeek();
					$update_stokcab['intid_week'] = $week[0]->intid_week;
					$update_stokcab['intid_barang'] = $arr_chk[0];
					$update_stokcab['intqty_begin'] = $qtyendbeforecab;
					$where_stokcab['intid_barang'] = $arr_chk[0];
					$where_stokcab['intid_cabang'] = $arr_chk[2];
					$this->Po_model->updateStokCab($where_stokcab, $update_stokcab);
					$this->Po_model->insertstokhistorycab($update_stokcab);
					}
					*/
					$id=0;
					$i =0;
					
					$id = $this->Po_model->insertSj($_POST);
					$pilih = $this->input->post('pilih');
					$qty = $this->input->post('quantity');
					$ket = $this->input->post('keterangan');
					$id_barang= $this->input->post('main');
					for($j=0;$j<sizeof($pilih);$j++) {
						$i = $pilih[$j];
						$quantity = $qty[$i];
						$keterangan = $ket[$i];
						$arr_chk = explode("_",$id_barang[$i]);
						//update PO
						$where_pr['intid_barang'] = $arr_chk[0];
						$where_pr['intid_po'] = $arr_chk[1];
						$update_pr['status'] = 1;
						$update_pr['qty_kirim'] = $quantity;
						$this->Po_model->updatePo($where_pr, $update_pr);
						//update SJ
						$update_po['is_sj'] = 1;
						$where_po['intid_po'] = $arr_chk[1];
						$this->Po_model->updatePoSj($where_po, $update_po);
						//Insert surat jalan detail
						$insert_sj['intid_barang']= $arr_chk[0];
						$insert_sj['intid_sj']= $id;
						$insert_sj['qty']= $quantity;
						$insert_sj['ket']= $keterangan;
						
						$this->Po_model->add_surat_jalanid($insert_sj);
						//berkurangnya stok pusat 
						//$qty = $arr_chk[3];
						//$where_getstok['intid_barang'] = $arr_chk[0];
						//$where_getstok['intid_cabang'] = 1;
						$intid_barang = $arr_chk[0];
						//$intid_cabang = 1;
						$get_stokpusat = $this->Po_model->get_stokpusat($intid_barang);
						//print_r($get_stokpusat)
						//$qtyendbefore = $get_stokpusat->row()->intqty_end;
						$qtyendbefore = $get_stokpusat[0]->intqty_end;
						//echo $qtyendbefore;
						$update_stokpusat['intqty_end'] = $qtyendbefore - $quantity;
						$update_stokpusat['tanggal']=  date("Y-m-d");
						$update_stokpusat['intqty_out']=  $quantity;
						$update_stokpusat['intid_cabang'] = 1;
						$week = $this->Penjualan_model->selectWeek();
						$update_stokpusat['intid_week'] = $week[0]->intid_week;
						$update_stokpusat['intid_barang'] = $arr_chk[0];
						$update_stokpusat['intqty_begin'] = $qtyendbefore;
						$where_stokpusat['intid_barang'] = $arr_chk[0];
						$where_stokpusat['intid_cabang'] = 1;
						$this->Po_model->updateStokPusat($where_stokpusat, $update_stokpusat);
						$this->Po_model->insertstokhistory($update_stokpusat);
						//bertambahnya stok cabang yang di kirim sj
						//$where_getstokcab['intid_barang'] = $arr_chk[0];
						//$qty = $arr_chk[3];
						//$update_stokcab['intid_cabang'] = $arr_chk[2];
						$intid_barang = $arr_chk[0];
						$intid_cabang = $arr_chk[2];
						$get_stokcab = $this->Po_model->get_stokcab($intid_barang, $intid_cabang);
						//$qtyendbefore = $get_stokcab->row()->intqty_end;
						$qtyendbeforecab = $get_stokcab[0]->intqty_end;
						$update_stokcab['intqty_end'] = $qtyendbeforecab + $quantity;
						$update_stokcab['tanggal']=  date("Y-m-d");
						$update_stokcab['intqty_in']=  $quantity;
						$update_stokcab['intid_cabang'] = $arr_chk[2];
						$week = $this->Penjualan_model->selectWeek();
						$update_stokcab['intid_week'] = $week[0]->intid_week;
						$update_stokcab['intid_barang'] = $arr_chk[0];
						$update_stokcab['intqty_begin'] = $qtyendbeforecab;
						$where_stokcab['intid_barang'] = $arr_chk[0];
						$where_stokcab['intid_cabang'] = $arr_chk[2];
						$this->Po_model->updateStokCab($where_stokcab, $update_stokcab);
						$this->Po_model->insertstokhistorycab($update_stokcab);
					
					}
		$data = $this->input->post('barang');
        for($i=1;$i<=sizeof($data);$i++){
			if(!empty($data[$i]['intid_id']) or $data[$i]['intquantity'] != 0 or $data[$i]['intquantity'] > 0 or !empty($data[$i]['intquantity'])){
				$detail = array(
								'intid_sj' 			=> $id,
								'intid_barang'	    => $data[$i]['intid_id'],
								'qty'		        => $data[$i]['intquantity']
				);
			$this->Po_model->add_surat_jalan($detail);
			$this->Penjualan_model->addStok($data[$i]['intid_id'], $data[$i]['intquantity'], 1, 0);
			$this->Po_model->tambahStok($data[$i]['intid_id'], $data[$i]['intquantity'], $this->input->post('intid_cabang'), 0);
			}
        }
		$data['default'] = $this->Po_model->get_cetak_surat_jalan($id);
		$data['week'] = $this->Penjualan_model->selectWeek();
		$data['id'] = $id;
		$this->load->view('admin_views/po/cetak_surat_jalan', $data);
	}
	
	function save_sjretur()
	{
		$id = $this->Po_model->insertSj($_POST);
		$data = $this->input->post('detail');
        for($i=1;$i<=sizeof($data);$i++){
		/*if(!empty($data[$i]['intid_id'])){
			$detail = array(
							'intid_sj' 			=> $id,
							'intid_barang'	    => $data[$i]['id_barang'],
							'qty'		        => $data[$i]['qty']
			);
		$this->Po_model->add_surat_jalan($detail);
	    $this->Penjualan_model->addStok($data[$i]['id_barang'], $data[$i]['qty'], 1, 0);
		$this->Po_model->tambahStok($data[$i]['id_barang'], $data[$i]['qty'], $this->input->post('intid_cabang'), 0);
		}*/
        }
		/*$data['default'] = $this->Po_model->get_cetak_surat_jalan($id);
		$data['week'] = $this->Penjualan_model->selectWeek();
		$this->load->view('admin_views/po/cetak_surat_jalan', $data);*/
	}
	
	function hutangbrg(){
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

        $data['cabang'] = $nm_cabang[0]->strnama_cabang;
		$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
		$week = $this->Laporan_model->selectWeek();
		foreach ($week as $g)
		{
			$data['id'][]	 		= $g->id;
			$data['intid_week'][] 	= $g->intid_week;
		}
		$this->load->view('admin_views/po/param_hutangbrg', $data);
	}
	
	function cetak_hutangbrg()
	{
		$data['week']= $this->input->post('intid_week');
		$nm_cabang = $this->Cabang_model->select($this->input->post('intid_cabang'));
        $data['cabang'] = $nm_cabang[0]->strnama_cabang;
		$data['default'] = $this->Po_model->get_HutangBarang($this->input->post('intid_week'), $this->input->post('intid_cabang'));
        $this->load->view('admin_views/po/hutang_barang_mingguan', $data);
	}
	
	function switchbarang()
	{
		if (!$_POST)
		{
			$page=$this->uri->segment(3);
			$batas=10;
			if(!$page):
			$offset = 0;
			else:
			$offset = $page;
			endif;
	
			$data['nama']="";
			$postkata = $this->input->post('nama');
			if(!empty($postkata))
			{
				$data['nama'] = $this->input->post('nama');
				$this->session->set_userdata('pencarian_barang', $data['nama']);
			}
			else
			{
				$data['nama'] = $this->session->userdata('pencarian_barang');
			}
			$data['nama_barang'] = $this->Barang_model->Cari_barang($batas,$offset,$data['nama']);
			$tot_hal = $this->Barang_model->tot_hal('barang','strnama',$data['nama']);
			$this->load->view('admin_views/po/switchbarang', $data);
		} else {
			$this->Po_model->EditNota($_POST);
		}
	}
	
	function lookupNoNota(){
		$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Po_model->selectNota($keyword);
        if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {
                $data['message'][] = array(
                                        'id'=> $row->intid_nota,
                                        'value' => $row->intno_nota,
                                        'value1' => $row->strnama_dealer,
                                        'value2' => $row->strnama_upline,
										'value3' => $row->strnama_unit,
                                     );
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data);
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	//line ikhlas firlana 26April2013
	//009
	/**
	* Untuk surat jalan sparepart dan item barang sparepart..
	* @param sjAdd_RSP()
	* @param buat_sj_sparepart()
	* @param save_sjpo_sparepart()
	*/
	//halaman yang menampilkan po sparepart
	function sjAdd_RSP(){		
		if ($_POST)
		{
		 	$data['default'] = $this->Po_model->selectSrsp($this->input->post('id_cabang'));
		 	$this->load->view('admin_views/po/sjAdd_RSP', $data);
		}else {
		    $this->load->view('admin_views/po/sjAdd_RSP');
 		}       
	}
	//halaman untuk membuat surat jalan sparepart
	function buat_sj_sparepart(){
		$jum = array();
		$i = 1;
		
		$nm_cabang = $this->Cabang_model->select($this->input->post('intid_cabang'));
        $data['cabang'] = $nm_cabang[0]->strnama_cabang;
		$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
		$getsj = $this->Po_model->get_MaxSjNew();
		$data['maxId'] = $getsj[0]->id;
		$nilai = $getsj[0]->id;
		$id = $nilai + 1;
		$this->Po_model->get_MaxSjUpdate($id);
		$data['week'] = $this->Penjualan_model->selectWeek();
		
		$detail =  $this->input->post('pilih');
		$x = implode(",", $detail);
		//echo $x.", week : ".$data['week'][0]->intid_week.", idcabang : ".$data['id_cabang'];
		//$data['default']    = $this->Po_model->get_retur_sparepart($data['id_cabang'],$data['week'][0]->intid_week);
		//echo ('id-cabang : '.$data['id_cabang'].', week : '.$data['week'][0]->intid_week);
		//$data['default']    = $this->Po_model->get_retur_sparepart($id_cabang,$week)($x,$data['id_cabang']);
		$data['default']	=	$this->Po_model->get_retur_surat_jalan($x,$data['id_cabang'],$data['week'][0]->intid_week);
		//////////////////////////////////
		/*
		foreach($data['default'] as $row){
			echo("".$this->input->post('intid_cabang').",intid_barang : ".$row->intid_barang."<br />");
			
			$jum[$i] = ;
			$i=$i+1;
		}
		*/
		$this->load->view('admin_views/po/sjCreate_sparepart', $data);
	}
	//halaman yang yang menyimpan surat jalan sparepart	
	function save_sjpo_sparepart()
	{
		
		$id = $this->Po_model->insertSj_SP($_POST);
		$id_barang = $this->input->post('pilih');
		$qty = $this->input->post('quantity');
		for($i=0;$i<sizeof($id_barang);$i++) {
			if(!empty($id_barang[$i])){
					//udah sukses
					$quantity = $qty[$i];
					$arr_chk = explode("_",$id_barang[$i]);
					//update PO
					$where_pr['intid_barang'] = $arr_chk[0];
					$where_pr['intid_retur_sparepart'] = $arr_chk[1];
					$update_pr['status'] = 1;
					$update_pr['qty'] = $quantity;
					$this->Po_model->updateSparepartdetail($where_pr, $update_pr);
					//echo("intid_retur_sparepart : ".$where_pr['intid_retur_sparepart'] .", intid_barang : ".$where_pr['intid_barang']."<br />");
					//echo("status : ".$update_pr['status'].", quantity : ".$update_pr['qty_kirim']."<br />");
							
					//update SJ
					$update_po['is_sj'] = 1;
					$where_po['intid_retur_sparepart'] = $arr_chk[1];
					$this->Po_model->updateSparepartSj($where_po, $update_po);
					
					//Insert surat jalan detail
					$insert_sj['intid_barang']= $arr_chk[0];
					$insert_sj['intid_sj']= $id;
					$insert_sj['qty']= $quantity;
					$this->Po_model->add_surat_jalanid($insert_sj);
					
					//berkurangnya stok pusat 
					//$qty = $arr_chk[3];
					//$where_getstok['intid_barang'] = $arr_chk[0];
					//$where_getstok['intid_cabang'] = 1;
					$intid_barang = $arr_chk[0];
					//$intid_cabang = 1;
					$get_stokpusat = $this->Po_model->get_stokpusat($intid_barang);
					//print_r($get_stokpusat)
					//$qtyendbefore = $get_stokpusat->row()->intqty_end;
					$qtyendbefore = $get_stokpusat[0]->intqty_end;
					//echo $qtyendbefore;
					$update_stokpusat['intqty_end'] = $qtyendbefore - $quantity;
					$update_stokpusat['tanggal']=  date("Y-m-d");
					$update_stokpusat['intqty_out']=  $quantity;
					$update_stokpusat['intid_cabang'] = 1;
					$week = $this->Penjualan_model->selectWeek();
					$update_stokpusat['intid_week'] = $week[0]->intid_week;
					$update_stokpusat['intid_barang'] = $arr_chk[0];
					$update_stokpusat['intqty_begin'] = $qtyendbefore;
					$where_stokpusat['intid_barang'] = $arr_chk[0];
					$where_stokpusat['intid_cabang'] = 1;
					//$this->Po_model->updateStokPusat($where_stokpusat, $update_stokpusat);
					//$this->Po_model->insertstokhistory($update_stokpusat);
					
					//bertambahnya stok cabang yang di kirim sj
					//$where_getstokcab['intid_barang'] = $arr_chk[0];
					//$qty = $arr_chk[3];
					//$update_stokcab['intid_cabang'] = $arr_chk[2];
					$intid_barang = $arr_chk[0];
					$intid_cabang = $arr_chk[2];
					$get_stokcab = $this->Po_model->get_stokcab($intid_barang, $intid_cabang);
					//$qtyendbefore = $get_stokcab->row()->intqty_end;
					$qtyendbeforecab = $get_stokcab[0]->intqty_end;
					$update_stokcab['intqty_end'] = $qtyendbeforecab + $quantity;
					$update_stokcab['tanggal']=  date("Y-m-d");
					$update_stokcab['intqty_in']=  $quantity;
					$update_stokcab['intid_cabang'] = $arr_chk[2];
					$week = $this->Penjualan_model->selectWeek();
					$update_stokcab['intid_week'] = $week[0]->intid_week;
					$update_stokcab['intid_barang'] = $arr_chk[0];
					$update_stokcab['intqty_begin'] = $qtyendbeforecab;
					$where_stokcab['intid_barang'] = $arr_chk[0];
					$where_stokcab['intid_cabang'] = $arr_chk[2];
					//$this->Po_model->updateStokCab($where_stokcab, $update_stokcab);
					//$this->Po_model->insertstokhistorycab($update_stokcab);
					}
				}
				$data = $this->input->post('barang');
				for($i=1;$i<=sizeof($data);$i++){
					if(!empty($data[$i]['intid_id'])){
						$detail = array(
										'intid_sj' 			=> $id,
										'intid_barang'	    => $data[$i]['intid_id'],
										'qty'		        => $data[$i]['intquantity']
						);
						$this->Po_model->add_surat_jalan($detail);
						$this->Po_model->add_Stok_history_Sparepart($this->input->post('no_srsp'),$week[0]->intid_week,$data[$i]['intid_id'],'1',$data[$i]['intquantity'],'keluar');
						$this->Po_model->add_Stok_history_Sparepart($this->input->post('no_srsp'),$week[0]->intid_week,$data[$i]['intid_id'],$arr_chk[2],$data[$i]['intquantity'],'masuk');				
					
					//$this->Penjualan_model->addStok($data[$i]['intid_id'], $data[$i]['intquantity'], 1, 0);
					//$this->Po_model->tambahStok($data[$i]['intid_id'], $data[$i]['intquantity'], $this->input->post('intid_cabang'), 0);
					}
				}
				echo $id;
				$data['default'] = $this->Po_model->get_cetak_surat_jalan($id);
				$data['week'] = $this->Penjualan_model->selectWeek();
				$this->load->view('admin_views/po/cetak_surat_jalan_sparepart', $data);
		}
	///line ikhlas 29April 2013
	/**
	* @param retursparepartAdd
	*/
	function retursparepartAdd(){
		if (!$_POST)
		{

            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;
			$data['week'] = $this->Penjualan_model->selectWeek();
			$week = $this->Po_model->selectWeek();
			foreach ($week as $g)
			{
				$data['id'][]	 	= $g->id;
				$data['intw'][] 	= $g->intid_week;
			}
			
			$getretur = $this->Po_model->get_MaxReturSparePart();
			$data['maxId'] = $getretur[0]->id;
			$nilai = $getretur[0]->id;
			$id = $nilai + 1;
			$this->Po_model->get_MaxReturSparePartUpdate($id);
            $data['user'] = $this->session->userdata('username');
			
			$this->load->view('admin_views/po/retur_add_reject', $data);
		 }else{
			$cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $ins = $this->Po_model->insert_surat_retur_sparepart($_POST);

            $id = $ins;
            //$id = "";
            $data = $this->input->post('barang');
            for($i=1;$i<=sizeof($data);$i++){
            if(!empty($data[$i]['intid_id'])){
            $detail = array(
                            'intid_retur_sparepart' 	=> $id,
                            'intid_barang'	=> $data[$i]['intid_id'],
                            'qty'		    => $data[$i]['intquantity'],
							'ket'			=> $data[$i]['ket']
                            );
			//echo('intid_retur : '.$id.', intid_barang : '.$data[$i]['intid_id'].', qty : '.$data[$i]['intquantity'].', ket : '.$data[$i]['ket'].'<br />');
             //var_dump($detail[$i]['intid_id']);
			 
				$this->Po_model->add_surat_retur_sparepart($detail);
			 //$this->Po_model->minusStok($data[$i]['intid_id'], $data[$i]['intquantity'], $this->input->post('intid_cabang'));
			 //$this->Po_model->addStokPusat($data[$i]['intid_id'], $data[$i]['intquantity']);		 
			//$this->Po_model->add_Stok_history_Sparepart($this->input->post('no_srsp'),$data[$i]['intid_id'],$cabang[0]->intid_cabang,$data[$i]['intquantity'],'keluar');
				}
            }

			redirect('po/cetak_surat_retur_sparepart/'.$id);
		}

	}
	function cetak_surat_retur_sparepart($id){
		$data['default'] = $this->Po_model->get_cetak_surat_retur_sparepart($id);
		$data['week'] = $this->Penjualan_model->selectWeek();
        $this->load->view('admin_views/po/cetak_surat_retur_sparepart', $data);	
	}
	function cetak_sttb_sparepart(){	
		$uri_segment = 3;
		$id = $this->uri->segment($uri_segment);
		//echo($id);
		$dataDetail = "";
		$data['default'] = $this->Po_model->get_CetakRetur_Sparepart($id);
		
		$data['week'] = $this->Penjualan_model->selectWeek();
		$data['surat_keluar'] = "surat_keluar";
		$cek = $this->Po_model->cek_sttb_sparepart($data['default'][0]->intid_retur_sparepart);		
		if($cek->no_sttb == ""){
			$getretur = $this->Po_model->get_MaxSttbNew();
			$data['maxId'] = $getretur[0]->id;
			$nilai = $getretur[0]->id;
			$id = $nilai + 1;
			$this->Po_model->get_MaxSttbUpdate($id);

				$sttb =  $id."/".$data['default'][0]->intid_retur_sparepart."/".$data['week'][0]->intid_week."/STTB/".date('m')."/".date('Y');
				$this->Po_model->update_sttb_sparepart($sttb,$data['default'][0]->intid_retur_sparepart);
				$dataDetail = $this->Po_model->get_all_sparepartdetail($data['default'][0]->intid_retur_sparepart);
				foreach($dataDetail as $row){
					$this->Po_model->add_Stok_history_Sparepart($row->no_srsp,$data['week'][0]->intid_week,$row->intid_barang,'1',$row->qty,'masuk');
					$this->Po_model->add_Stok_history_Sparepart($row->no_srsp,$data['week'][0]->intid_week,$row->intid_barang,$row->intid_cabang,$row->qty,'keluar');				
				}
				//echo "berhasil";
		}
        $this->load->view('admin_views/po/cetak_surat_retur_sparepart', $data);
			
			//echo 'offset : '.$offset;
	}
	function sreturpusat_rejec(){
		$this->load->library('pagination'); 
		$uri_segment = 4;
		$offset = $this->uri->segment($uri_segment);
		$config['base_url'] = site_url('po/sreturpusat_rejec/');
		$config['total_rows'] = $this->Po_model->countDataReturSparepart();
		$config['per_page'] = $this->limit;
		$config['num_links'] = 2;
		$config['full_tag_open'] = '<div id="pagination">';
		$config['full_tag_close'] = '</div>';

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		$data['offset']	= $offset;
		$data['jumlah_data']=$this->Po_model->countDataRetur();
		
		if($_POST){
			$data['po']	= $this->Po_model->get_list_data_filter_retur_sparepart($this->limit,$offset, $_POST);	
		}else{
			$data['po']	= $this->Po_model->get_list_retur_data_sparepart($this->limit,$offset);
		}
            $cabang = $this->User_model->getCabang($this->session->userdata('username'));
            $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            $data['cabang'] = $nm_cabang[0]->strnama_cabang;
            $data['id_cabang'] = $nm_cabang[0]->intid_cabang;


            $data['user'] = $this->session->userdata('username');
        $this->load->view('admin_views/po/returpusat_sparepart', $data);
	}
	function lookupSparePart(){
		$keyword = $this->input->post('term');
			$data['response'] = 'false';
			$query = $this->Po_model->selectBarangSparePart($keyword);
			if( ! empty($query) )
			{
				$data['response'] = 'true';
				$data['message'] = array();
				foreach( $query as $row )
				{
					$data['message'][] = array(
											'id'=>$row->id_sparepart,
											'value' => $row->strnama
										 );
				}
			}
			if('IS_AJAX')
			{
				echo json_encode($data);
			}
			else
			{
				$this->load->view('admin_views/autocomplete/index',$data);
			}
		}
	function srpusat(){
	
		$this->load->view('admin_views/po/srpusat');
	}
	//line ikhlas22April2013
	function lookupBarangPO(){
	$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Po_model->selectBarangPO($keyword);
        if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
                                        'value' => $row->strnama
                                     );
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data);
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	function Coba_kirim(){
		 $this->load->view('admin_views/po/test_kirim');
	}
	function terima_kirim(){
		/*
		$id_barang = $this->input->post('pilih');
		for($i=0;$i<sizeof($id_barang);$i++) {		
			echo $id_barang[$i];
		}
		/*
		$data = $this->input->post('barang');
        for($i=1;$i<=sizeof($data);$i++){
		if(!empty($data[$i]['intid_id'])){
			$detail = array(
							'intid_sj' 			=> $id,
							'intid_barang'	    => $data[$i]['intid_id'],
							'qty'		        => $data[$i]['intquantity']
			);
		$this->Po_model->add_surat_jalan($detail);
	    $this->Penjualan_model->addStok($data[$i]['intid_id'], $data[$i]['intquantity'], 1, 0);
		$this->Po_model->tambahStok($data[$i]['intid_id'], $data[$i]['intquantity'], $this->input->post('intid_cabang'), 0);
		}
        }
		$data['default'] = $this->Po_model->get_cetak_surat_jalan($id);
		$data['week'] = $this->Penjualan_model->selectWeek();
		$this->load->view('admin_views/po/cetak_surat_jalan', $data);
		*/
		$id=0;
		$i =0;

		$pilih = $this->input->post('pilih');
		$qty = $this->input->post('quantity');
		$ket = $this->input->post('keterangan');
		$id_barang= $this->input->post('main');
		for($j=0;$j<sizeof($pilih);$j++) {
		echo "".$pilih[$j]."<br>";
	
							//$i = ;
							$quantity = $qty[$pilih[$j]];
							$keterangan = $ket[$pilih[$j]];
							/*
							$arr_chk = explode("_",$id_barang[$i]);
							//update PO
							$where_pr['intid_barang'] = $arr_chk[0];
							$where_pr['intid_po'] = $arr_chk[1];
							$update_pr['status'] = 1;
							$update_pr['qty_kirim'] = $quantity;
							//$this->Po_model->updatePo($where_pr, $update_pr);
							//update SJ
							$update_po['is_sj'] = 1;
							$where_po['intid_po'] = $arr_chk[1];
							//$this->Po_model->updatePoSj($where_po, $update_po);
							//Insert surat jalan detail
							$insert_sj['intid_barang']= $arr_chk[0];
							$insert_sj['intid_sj']= $id;
							$insert_sj['qty']= $quantity;
							$insert_sj['ket']= $keterangan;
							
							//$this->Po_model->add_surat_jalanid($insert_sj);
							//berkurangnya stok pusat 
							//$qty = $arr_chk[3];
							//$where_getstok['intid_barang'] = $arr_chk[0];
							//$where_getstok['intid_cabang'] = 1;
							$intid_barang = $arr_chk[0];
							//$intid_cabang = 1;
							$get_stokpusat = $this->Po_model->get_stokpusat($intid_barang);
							//print_r($get_stokpusat)
							//$qtyendbefore = $get_stokpusat->row()->intqty_end;
							$qtyendbefore = $get_stokpusat[0]->intqty_end;
							//echo $qtyendbefore;
							$update_stokpusat['intqty_end'] = $qtyendbefore - $quantity;
							$update_stokpusat['tanggal']=  date("Y-m-d");
							$update_stokpusat['intqty_out']=  $quantity;
							$update_stokpusat['intid_cabang'] = 1;
							$week = $this->Penjualan_model->selectWeek();
							$update_stokpusat['intid_week'] = $week[0]->intid_week;
							$update_stokpusat['intid_barang'] = $arr_chk[0];
							$update_stokpusat['intqty_begin'] = $qtyendbefore;
							$where_stokpusat['intid_barang'] = $arr_chk[0];
							$where_stokpusat['intid_cabang'] = 1;
							//$this->Po_model->updateStokPusat($where_stokpusat, $update_stokpusat);
							//$this->Po_model->insertstokhistory($update_stokpusat);
							//bertambahnya stok cabang yang di kirim sj
							//$where_getstokcab['intid_barang'] = $arr_chk[0];
							//$qty = $arr_chk[3];
							//$update_stokcab['intid_cabang'] = $arr_chk[2];
							$intid_barang = $arr_chk[0];
							$intid_cabang = $arr_chk[2];
							$get_stokcab = $this->Po_model->get_stokcab($intid_barang, $intid_cabang);
							//$qtyendbefore = $get_stokcab->row()->intqty_end;
							$qtyendbeforecab = $get_stokcab[0]->intqty_end;
							$update_stokcab['intqty_end'] = $qtyendbeforecab + $quantity;
							$update_stokcab['tanggal']=  date("Y-m-d");
							$update_stokcab['intqty_in']=  $quantity;
							$update_stokcab['intid_cabang'] = $arr_chk[2];
							$week = $this->Penjualan_model->selectWeek();
							$update_stokcab['intid_week'] = $week[0]->intid_week;
							$update_stokcab['intid_barang'] = $arr_chk[0];
							$update_stokcab['intqty_begin'] = $qtyendbeforecab;
							$where_stokcab['intid_barang'] = $arr_chk[0];
							$where_stokcab['intid_cabang'] = $arr_chk[2];
							//$this->Po_model->updateStokCab($where_stokcab, $update_stokcab);
							//$this->Po_model->insertstokhistorycab($update_stokcab);
							
							echo "i: ".$i.",quantity: ". $qty[$i].", id_barang : ".$arr_chk[0].",ketrangan : ".$ket[$i]."<br />";
							*/
							}
					
	}
	//end
	function print_excel_surat($id) {
		$data['default'] = $this->Po_model->get_cetak_surat_jalan($id);
		$data['week'] = $this->Penjualan_model->selectWeek();
		$data['id'] =	$id;
		$this->load->view('admin_views/po/cetak_surat_jalan_excel', $data);
    }
	
//look all po
//line ikhlas
	/*
	* @param po_look
	* desc :  menampilkan semua po dari cabang-cabang tertentu
	*/
	function po_look(){
		$cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
		
		$data['pagination'] = "";
		$data['po'] = $this->Po_model->get_PO_cabang($cabang[0]->intid_cabang);
		$this->load->view('admin_views/po/lookup_po_cabang',$data);
	}
}
?>
